﻿using Common;
using Model;
using Service;
using Service.Impl;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace NetManagerSystem
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        private CommandDispatcher CommandDispatcher;

        public MainWindow()
        {
            InitializeComponent();
            CommonObject.CommandContext = new CommandContext();
            CommonObject.CommandContext.Hostname = "Gujimeng-PC";
            CommonObject.CommandContext.Interface = "wifi";
            CommonObject.CommandContext.EnterView(CommandView.COMMAND_VIEW_GLOBAL);
            CommandDispatcher = new CommandDispatcher();
            cmdMain.GetHeaderText += CmdMain_GetHeaderText;
            PrintWelcome();
            cmdMain.write("程序启动中，正在注册所有命令。。。");
            cmdMain.write();
            CommandDispatcher.RegisterCommands();
            cmdMain.writeHeaderText();
            cmdMain.PressKey += CmdMain_PressKey;
        }
        public void PrintWelcome()
        {
            cmdMain.write("计算机网络管理系统(Computer Network Manager System)");
            cmdMain.write("命令行方式管理您的计算机网络");
            cmdMain.write();
        }
        private void CmdMain_PressKey(KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                if (!string.IsNullOrEmpty(cmdMain.Command))
                {
                    var cpr = CommandDispatcher.ParseRunCommand(cmdMain.Command);
                    if (cpr.Success)
                    {
                        if (!string.IsNullOrEmpty(cpr.Description))
                        {
                            Console.WriteLine(cpr.Description);
                            cmdMain.write(cpr.Description);
                        }
                    }
                    else
                    {
                        cmdMain.write(cpr.Description);
                    }
                }
            }
            if (e.Key == Key.OemQuestion && e.KeyboardDevice.Modifiers == ModifierKeys.Shift)
            {
                // 按下SHIFT_?
                CommandNextInputResult cnir = CommandDispatcher.ParseNextInputCommand(cmdMain.Command);
                if (cnir.Success)
                {
                    foreach(var nv in cnir.NextValues)
                    {
                        cmdMain.write($"{nv.Key}\t\t{nv.Value}");
                    }
                }
                else
                {
                    cmdMain.write(cnir.Description);
                }
            }
            if (e.Key == Key.Tab)
            {
                // 按下SHIFT_?
                CommandNextInputResult cnir = CommandDispatcher.ParseNextInputCommand(cmdMain.Command);
                if (cnir.Success)
                {
                    cmdMain.write();
                    foreach (var nv in cnir.NextValues)
                    {
                        cmdMain.write($"{nv.Key} ", end: "");
                    }
                    cmdMain.write();
                    cmdMain.write();
                }
                else
                {
                    cmdMain.write(cnir.Description);
                }
                cmdMain.writeHeaderText();
                cmdMain.write(cmdMain.Command, end:"");
            }
        }
        private string CmdMain_GetHeaderText()
        {
            return CommonObject.CommandContext.HeaderText;
        }
    }
}
